Skip to content

Conversation

dogacancolak
Copy link

@dogacancolak dogacancolak commented Aug 29, 2025

Fix OAuth scope handling to prioritize PRM spec's scopes_supported over Authorization Server Metadata spec's scopes_supported, ensuring clients only request resource-specific scopes and preventing authorization rejections in multi-resource environments.

Motivation and Context

Currently, the MCP client prioritizes all scopes from the OAuth authorization server metadata endpoint, which can cause issues in multi-resource environments. This update reverses the priority order: it first checks the Protected Resource Metadata (PRM) endpoint's scopes_supported field to obtain only the necessary scopes for the specific resource, and only falls back to the authorization server metadata if needed.

This addresses issues such as:

  1. Authorization servers supporting scopes for multiple resources (e.g., "resource1:read" and "resource2:read").
  2. Clients not being authorized for all advertised scopes, resulting in rejected authorization requests.

How Has This Been Tested?

Comprehensive test cases were added in tests/client/test_auth.py to cover various scope handling scenarios:

  • PRM scopes are prioritized over OAuth metadata scopes.
  • OAuth metadata scopes are used as fallback when PRM has no scopes.
  • No scope changes when both PRM and OAuth metadata lack scopes.
  • Existing client scope is preserved regardless of metadata.

Breaking Changes

This is a breaking change for applications relying on the previous scope handling behavior. The new implementation aligns with the PRM specification, which states that scopes_supported contains "scope values that are used in authorization requests to request access to this protected resource."


Types of Changes

  • Bug fix: Non-breaking change which fixes an issue.
  • New feature: Non-breaking change which adds functionality.
  • Breaking change: Fix or feature that would cause existing functionality to change.
  • Documentation update

Checklist

  • I have read the Model Context Protocol documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional Context

The core change modifies the _handle_oauth_metadata_response method in src/mcp/client/auth.py to properly prioritize scopes from PRM over OAuth metadata. This ensures clients only request scopes relevant to the specific resource, improving security and reducing authorization rejections.

@jennsun
Copy link

jennsun commented Sep 8, 2025

Can we confirm if updating the Python SDK will update the Claude Client, or will we also need to make updates in the TypeScript SDK for these to reflect in Claude.ai? In the Typescript SDK, for example, we are not retrieving scopes from the PRM endpoint: https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/client/auth.ts#L328
CC: @pcarleton

@felixweinberger felixweinberger added needs more eyes Needs alignment among maintainers whether this is something we want to add auth Issues and PRs related to Authentication / OAuth labels Sep 24, 2025
Comment on lines +491 to +492
elif metadata.scopes_supported is not None:
self.context.client_metadata.scope = " ".join(metadata.scopes_supported)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this latest change to the spec:

When implementing authorization flows, MCP clients SHOULD follow the principle of least privilege by requesting
only the scopes necessary for their intended operations. During the initial authorization handshake, MCP clients
SHOULD follow this priority order for scope selection:

  1. Use scope parameter from the initial WWW-Authenticate header in the 401 response, if provided
  2. If scope is not available, use all scopes defined in scopes_supported from the Protected Resource Metadata document, omitting the scope parameter if scopes_supported is undefined.

This approach accommodates the general-purpose nature of MCP clients, which typically lack domain-specific knowledge to make informed decisions about individual scope selection. Requesting all available scopes allows the authorization server and end-user to determine appropriate permissions during the consent process.

Are you able to update this PR to:

  1. Get scope from www-authenticate
  2. Omit scope if not present in PRM? (currently impl falls back to scopes_supported from AS metadata)

@felixweinberger felixweinberger added needs more work Not ready to be merged yet, needs additional changes. and removed needs more eyes Needs alignment among maintainers whether this is something we want to add labels Oct 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auth Issues and PRs related to Authentication / OAuth needs more work Not ready to be merged yet, needs additional changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants